package com.atguigu.srb.core.controller.api;


import com.atguigu.srb.base.result.ResponseVo;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.entity.Borrower;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.service.BorrowerService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 借款人 前端控制器
 * </p>
 *
 * @author muzhao
 * @since 2024-08-19
 */
@RestController
@RequestMapping("/api/core/borrower")
public class ApiBorrowerController {
    @Resource
    BorrowerService borrowerService;

    @ApiOperation("查询借款人申请状态：（0：未认证，1：认证中， 2：认证通过， -1：认证失败）")
    @GetMapping("status")
    public ResponseVo borrowerStatus(@RequestHeader(value = "token", required = false) String token) {
        Long userId = JwtUtils.getUserId(token);
        //一个会员只能提交一个借款人信息： 1:1
        Borrower borrower = borrowerService.getOne(Wrappers.lambdaQuery(Borrower.class)
                .eq(Borrower::getUserId, userId));
        return ResponseVo.ok().data("status",borrower==null?0:borrower.getStatus());
    }

    @ApiOperation("提交借款人数据")
    @PostMapping
    public ResponseVo save(@RequestBody BorrowerVO borrowerVO,
                           @RequestHeader(value = "token", required = false) String token
    ) {
        //自定义vo接收前端提交的数据
        //token + borrower + borrowerAttach
        borrowerService.saveBorrowerVO(borrowerVO,token);
        return ResponseVo.ok().message("保存成功");
    }
}

